Skip to content

fix(mcpx): render daemon environment as [Service] Environment= entries - #5100

Merged
developing-today[bot] merged 1 commit into
mainfrom
fix-mcpx-unit-environment
Nov 3, 2026
Merged

developing-today[bot] merged 1 commit into
mainfrom
fix-mcpx-unit-environment

Conversation

@developing-today

@developing-today developing-today Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Refs #5088. (It closes when the fix has ridden the pin cascade and a deploy passes — see the last section.)

The bug

Cascade deploy round 3 (2026-11-03) built home.activationPackage green from code@8c065075 but aborted in reloadSystemd: sd-switch could not parse the generated mcpx.service.

Error: Error switching systemd units
Caused by:
    0: Failed to build switch plan
    1: Failed to load unit file .../home-files/.config/systemd/user/mcpx.service
    2: .../mcpx.service: expected '=' but got '_' at line 20, column 11

pkgs/mcpx/modules/home-manager.nix set the daemon environment at the service level of systemd.user.services.mcpx (inherit (cfg) environment;). Home Manager's unit type carries a freeform fallback, so the unknown attribute was serialized verbatim as a section — the generated unit literally contained a [environment] section holding ANTIGRAVITY_BIN, CLAUDE_BIN, CODEX_BIN, OPENCODE_BINARY, OPENCODE_V2_BIN and PATH.

sd-switch (the parser in HM's activation path) rejects that. systemd's own parser is lenient — systemd-analyze verify on the round-3 unit prints Unknown section 'environment'. Ignoring. and exits 0 — so anywhere sd-switch does not gate, the unit would load with the configured environment silently dropped.

The fix

One file, pkgs/mcpx/modules/home-manager.nix: the service-level attribute is gone, and cfg.environment is rendered into Home Manager's typed per-service option instead:

Service.Environment = lib.mapAttrsToList (name: value: "${name}=${value}") cfg.environment;

Service.Environment is a real option in HM's serviceType (coercedTo str lib.toList (listOf str)) — the mechanism HM provides for exactly this. Values are emitted verbatim, one Environment= entry per variable, PATH included exactly as configured (no merging or rewriting). The services.mcpx.environment option's type and default are unchanged; the darwin branch is untouched (launchd's EnvironmentVariables is a real key), as is the NixOS sibling module (systemd.services has a genuine service-level environment option).

Premise re-test: the old comment was wrong

The comment this replaces claimed Service.Environment was avoided because "home-manager injects its own Service.Environment (session variables) and the two definitions do not merge". Re-tested against the pinned home-manager source and by evaluation; both clauses fail:

  • No injection exists. HM core never sets Service.Environment on user services. Session variables travel a different channel: systemd.user.sessionVariables are written to the user manager's environment.d/10-home-manager.conf, and units inherit the manager environment at runtime; unit-level Environment= lines apply on top. In the amd configuration, systemd.user.services.mcpx.Service.Environment evaluated to [] before this fix — nothing else contributes entries for them to clash with — while the config's seven systemd.user.sessionVariables sit in the manager channel, unaffected either way.
  • List definitions merge. In a minimal two-module config (the mcpx module with services.mcpx.environment = { MCPX_TEST_VAR = "from-module-config"; }, plus an independent systemd.user.services.mcpx.Service.Environment = [ "EXTRA_INJECTED=from-second-definition" ] definition), the evaluated result after this fix is ["MCPX_TEST_VAR=from-module-config", "EXTRA_INJECTED=from-second-definition"] — concatenation, as the module system does for listOf. (Before the fix the same config yields only the second entry: the module contributed nothing to Service.Environment.)

So Service.Environment was always safe; the module comment now records the corrected reasoning.

Evidence

The amd config consumes this module through the pin cascade (code's openchamber input → its opencode input → the fork's mcpx-src, currently code@d51d7bef), so — as with PR #5037 — the render/build proofs run with --override-input openchamber/opencode/mcpx-src pointed at this PR's worktree, simulating the post-cascade state. (Consequence: the mcpx package store path in the rendered unit is a fresh build, pahg1lq…-mcpx-0.1.0, not production's bylp1b5…; every configured environment value is identical.)

  • Evaluated service: top-level keys are exactly Install, Service, Unit — no environment attribute. Service.Environment holds the six configured entries; a byte-exact comparison against the round-3 baseline values (all six, PATH included) passes.
  • Rendered unit (from the built generation): [Service] opens with six Environment= lines — ANTIGRAVITY_BIN, CLAUDE_BIN, CODEX_BIN, OPENCODE_BINARY, OPENCODE_V2_BIN, PATH, each with its configured store-path value — and there is no [environment] section.
  • Same-class audit: opencode.service and openchamber.service rendered from the same config are clean, and a census of every section header in every unit file of the built generation finds only [Unit], [Service], [Install], [Timer], [Socket], [Path] — no stray sections anywhere.
  • Parse: sd-switch 0.6.4 (the version HM's reloadSystemd invokes) in --dry-run with an empty old-units dir: with the round-3 unit as the new unit it fails with the exact round-3 error (expected '=' but got '_' at line 20, column 11); with the fixed unit it builds the switch plan (Stopping units: mcpx.service / Starting units: mcpx.service) with no error. systemd-analyze verify on the fixed unit is silent, exit 0.
  • Build: home.activationPackage builds green at this head (with the override above).

Landing

This PR changes the module at its origin (code). Before deploy round 4 can pick it up, the fix must ride the pin cascade: the fork bumps mcpx-src past this merge, openchamber bumps its opencode pin, and code bumps its openchamber pin. #5088 stays open until a deploy with the cascaded fix passes reloadSystemd and the running daemon's /proc/<pid>/environ shows the configured keys.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

The home-manager module passed services.mcpx.environment through at the service level of systemd.user.services.mcpx, where the unit type freeform serialized it as a literal [environment] section. sd-switch refuses to parse the generated unit (the cascade deploy round-3 abort, code#5088), and systemd's own parser tolerates and silently drops the unknown section — the daemon environment with it.

Render cfg.environment into home-manager's typed Service.Environment list instead (lib.mapAttrsToList to NAME=value entries, values verbatim). The old comment's premise does not hold: home-manager injects no per-service Service.Environment (session variables reach units through the user manager's environment.d), and list definitions concatenate in the module system. Both re-tested; evidence in the PR.
@developing-today
developing-today Bot requested review from a team and dezren39 as code owners November 3, 2026 19:47
@coderabbitai

coderabbitai Bot commented Nov 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a5c201f4-5c90-42c4-afb2-3333695388b0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants